package com.iuie.ns.datum.service.impl;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.iuie.ns.datum.dao.IFileInfoDao;
import com.iuie.ns.datum.mapper.FileInfoMapper;
import com.iuie.ns.datum.service.IFileInfoService;
import com.iuie.ns.datum.vo.FileInfoVO;
import com.iuie.ns.datum.vo.FileTagVO;
import com.iuie.ns.frame.persistence.dao.IGenericDao;
import com.iuie.ns.frame.service.impl.BaseServiceImpl;
import com.iuie.ns.frame.utils.StringUtils;
import com.iuie.ns.frame.web.dataset.vo.Page;

@Service("fileInfoService")
@Transactional
public class FileInfoServiceImpl extends BaseServiceImpl<FileInfoVO> implements IFileInfoService {

	@Resource
	private IFileInfoDao fileInfoDao;
	
	@Resource
	private FileInfoMapper fileInfoMapper;
	
	@Override
	public List<FileInfoVO> queryLst() {
		@SuppressWarnings("unchecked")
		List<FileInfoVO> list =fileInfoDao.createQuery("from FileInfoVO order by uploadTime desc").list();
		return list;
	}

	@Override
	public void downloadFileById(Serializable fileId) {
		fileInfoMapper.downloadFileById(fileId);
	}

	public List<FileInfoVO> queryLst(final Page page) {
		return this.queryLst(page, null);
	}

	@Override
	public List<FileInfoVO> queryLst(final Page page, final FileInfoVO fileInfo) {
		if (page == null) {
			return super.findAll();
		}
		PageHelper.startPage(page.getPage(), page.getRows(), true);
		List<FileInfoVO> list = fileInfoMapper.queryLst(fileInfo);
		page.loadData(list);
		return list;
	}
	
	@Override
	protected IGenericDao<FileInfoVO, Serializable> getGenericDao() {
		return fileInfoDao;
	}

	@Override
	public List<FileInfoVO> findByTag(Page page, FileTagVO fileTag) {
		return fileInfoDao.findByTag(page, fileTag);
	}
	
	@Override
	public void addFileInfo(FileInfoVO fileInfo, boolean commit) {
		fileInfoDao.addFileInfo(fileInfo, commit);
	}

	@Override
	public String renameFile(Long fileId, String fileName) {
		FileInfoVO fileInfo = this.findById(fileId);
		String extension = FilenameUtils.getExtension(fileInfo.getOriginalName());
		String newFileName = fileName + "." + extension;
		fileInfoMapper.updateFileName(fileId, newFileName);
		return FilenameUtils.getBaseName(fileInfo.getOriginalName());
	}
	
	@Override
	public List<FileInfoVO> findByTargetId(String targetId) {
		if (StringUtils.isBlank(targetId)) {
			return null;
		}
		FileInfoVO condition = new FileInfoVO();
		condition.setTargetId(targetId);
		return fileInfoMapper.queryLst(condition);
	}
	
	@Override
	public Integer updateTargetId(Long fileId, String targetId) {
		return fileInfoMapper.updateTargetId(fileId, targetId);
	}
	
	@Override
	public Integer deleteByTargetId(String targetId) {
		return fileInfoMapper.deleteByTargetId(targetId);
	}
	
	@Override
	public void deleteById(Serializable fileId) {
		fileInfoMapper.deleteById((Long) fileId);
	}
	
	@Override
	public FileInfoVO findById(Long fileId) {
		return fileInfoMapper.findById(fileId);
	}
}
